#include "sceneslider.h"
#include <QtMath>

SceneSlider::SceneSlider ( QWidget* parent ) : QQtLongClickWidget ( parent )
{
    connect ( this, SIGNAL ( longClickWithPoint ( QPoint ) ),
              this, SLOT ( longClicked ( QPoint ) ) );

    for ( int i = 0; i < 2; i++ )
    {
        QQtDictionary* pnode = new QQtDictionary();
        QQtDictionary& node = *pnode;

        node["point"].setValue<QPoint> ( QPoint ( 0, 0 ) );
        node["color"].setValue<QRgb> ( 0 );

        LingWidget* w = new LingWidget ( this );
        w->hide();
        w->setColor ( QColor ( Qt::black ) );
        node["widget"].setValue<QWidget*> ( w );

        connect ( w, SIGNAL ( click() ),
                  this, SLOT ( slotLingClicked() ) );

        mLingNode2.addChild ( node );
    }
}

bool getMinNode ( const QQtDictionary& prev, const QQtDictionary& next )
{
    QPoint prevPoint = prev["point"].getValue().value<QPoint>();
    QPoint nextPoint = next["point"].getValue().value<QPoint>();
    return prevPoint.x() < nextPoint.x();
}

void SceneSlider::longClicked ( QPoint point )
{
    //这里完全不必要使用new,资源浪费.
    QQtDictionary* pnode = new QQtDictionary();
    QQtDictionary& node = *pnode;
    node["point"].setValue<QPoint> ( point );
    node["color"].setValue<QRgb> ( 0 );

    LingWidget* w = new LingWidget ( this );
    w->setColor ( QColor ( Qt::black ) );
    node["widget"].setValue<QWidget*> ( w );

    connect ( w, SIGNAL ( click() ),
              this, SLOT ( slotLingClicked() ) );
    connect ( w, SIGNAL ( longClick() ),
              this, SLOT ( deleteLing() ) );

    mLingNode.addChild ( node );
    qSort ( mLingNode.getList().begin(),
            mLingNode.getList().end(),
            getMinNode );

    update();
}

void SceneSlider::slotLingClicked()
{
    LingWidget* obj = ( LingWidget* ) sender();
    emit lingClicked ( obj );
}

void SceneSlider::deleteLing ( )
{
    QObject* obj =  sender();
    LingWidget* w = ( LingWidget* ) obj;
    int i = 0;
    for ( ; i < mLingNode.count(); i++ )
    {
        QWidget* w0 = mLingNode.getList() [i]["widget"].getValue().value<QWidget*>();
        if ( w0 == ( QWidget* ) w )
        {
            w0->deleteLater();
            break;
        }
    }
    mLingNode.remove ( i );
    update();
}

void SceneSlider::paintEvent ( QPaintEvent* event )
{
    QPainter painter ( this );

    //显示起点和终点
    mLingNode2[0]["point"].setValue<QPoint> ( QPoint ( 0, 0 ) );
    QWidget* widget = mLingNode2[0]["widget"].getValue().value<QWidget*>();
    widget->show();
    widget->setGeometry ( 0, 0, 10, 10 );

    mLingNode2[1]["point"].setValue<QPoint> ( QPoint ( width(), height() ) );
    widget = mLingNode2[1]["widget"].getValue().value<QWidget*>();
    widget->show();
    widget->setGeometry ( width() - 10, 0, 10, 10 );

    //显示中间的点
    for ( int i = 0; i < mLingNode.count(); i++ )
    {
        QPoint point = mLingNode[i]["point"].getValue().value<QPoint>();
        widget = mLingNode[i]["widget"].getValue().value<QWidget*>();
        widget->show();
        widget->setContentsMargins ( 0, 0, 0, 0 );
        widget->setGeometry ( point.x(), 0, 10, 10 );
    }

    //画横线
    painter.fillRect ( QRect ( 5, 10, width() - 10, 5 ), QBrush ( QColor ( Qt::black ) ) );

    //画gradient
    QLinearGradient gradient;

    QColor prevColor;
    QPoint prevPoint;

    QColor nowColor;
    QPoint nowPoint;

    if ( mLingNode.count() == 0 )
    {

    }
    else
    {

    }

    //list默认数目为-1
    //pline() << mLingNode.count();
    int count = mLingNode.count();
    if ( count < 0 ) count = 0;

    //全面绘制与前一点的关系
    for ( int i = 0; i < count + 1; i++ )
    {
        //绘制和起点的关系
        if ( i == 0 ) //first
        {
            prevPoint = mLingNode2[0]["point"].getValue().value<QPoint>();
            prevColor = QColor ( mLingNode2[0]["color"].getValue().value<QRgb>() );

            if ( count <= 0 )
            {
                nowPoint = mLingNode2[1]["point"].getValue().value<QPoint>();
                nowColor = QColor ( mLingNode2[1]["color"].getValue().value<QRgb>() );
            }
            else
            {
                nowPoint = mLingNode[i]["point"].getValue().value<QPoint>();
                nowColor = QColor ( mLingNode[i]["color"].getValue().value<QRgb>() );
            }
        }

        //绘制终点和前一点的关系
        else if ( i == count ) //last
        {
            if ( count <= 0 )
            {
                prevPoint = mLingNode2[0]["point"].getValue().value<QPoint>();
                prevColor = QColor ( mLingNode2[0]["color"].getValue().value<QRgb>() );
            }
            else
            {
                prevPoint = mLingNode[i - 1]["point"].getValue().value<QPoint>();
                prevColor = QColor ( mLingNode[i - 1]["color"].getValue().value<QRgb>() );
            }

            nowPoint = mLingNode2[1]["point"].getValue().value<QPoint>();
            nowColor = QColor ( mLingNode2[1]["color"].getValue().value<QRgb>() );
        }

        //绘制和前一个点的关系
        else
        {
            prevPoint = mLingNode[i - 1]["point"].getValue().value<QPoint>();
            prevColor = QColor ( mLingNode[i - 1]["color"].getValue().value<QRgb>() );

            nowPoint = mLingNode[i]["point"].getValue().value<QPoint>();
            nowColor = QColor ( mLingNode[i]["color"].getValue().value<QRgb>() );
        }

        //prevColor = nowColor;
        //prevPoint = nowPoint;

        gradient.setStart ( prevPoint );
        gradient.setColorAt ( 0, prevColor );

        gradient.setFinalStop ( nowPoint );
        //nowColor = qrand() % 0xFFFFFF;
        gradient.setColorAt ( 1, nowColor );

        //pline() << prevPoint << nowPoint;

        painter.setBrush ( QBrush ( gradient ) );
        if ( i == count ) //last
        {
            painter.drawRect ( prevPoint.x() + 5, 20, nowPoint.x() - prevPoint.x() - 10, 40 );
        }
        else
        {
            painter.drawRect ( prevPoint.x() + 5, 20, nowPoint.x() - prevPoint.x(), 40 );
        }
    }
}


void LingWidget::paintEvent ( QPaintEvent* event )
{
    QPainter painter ( this );

    painter.save();
    painter.setRenderHints ( QPainter::Antialiasing | QPainter::TextAntialiasing );
    painter.translate ( width() / 2, 0 );
    painter.rotate ( 45 );

    //-100, -100; 100, 100
    //菱形
    painter.setBrush ( QBrush ( QColor ( mColor ) ) );
    qreal xxx = qSqrt ( qPow ( width() / 2, 2 ) + qPow ( height() / 2, 2 ) );
    painter.drawRect ( 0, 0, xxx, xxx );
    //painter.fillRect ( -width() / 2, -height() / 2, width(), height(), QBrush ( QColor ( mColor ) ) );
    painter.restore();
}
